package com.weblog.article.controller;

import entity.ExceptionResult;
import enums.ExceptionEnum;
import exception.MyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
 */
@ControllerAdvice
public class CommonExceptionHandler {
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public ResponseEntity<ExceptionResult> handleException(MyException e){
        ExceptionEnum em = e.getExceptionEnum();
        return ResponseEntity.status(em.getCode())
                .body(new ExceptionResult(e.getExceptionEnum()));
    }
}
